package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.MessageBean3;
import com.wnhz.work.yz.bean.OrderBean;
import com.wnhz.work.yz.bean.OrderDetailsBean;
import com.wnhz.work.yz.timecountown.TimeViewFlicker;
import com.wnhz.work.yz.view.NoScrollListView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2016/12/15.
 */
public class OrderAdapter extends BaseAdapter {
    private Context context;
    private List<OrderBean> beanList = new ArrayList<>();
    private View.OnClickListener listener;
    private OrderContentAdapter mAdapter;

    private List<OrderDetailsBean> data;
    private int index;

    public OrderAdapter(Context context, List<OrderBean> beanList, View.OnClickListener listener) {
        this.context = context;
        this.listener = listener;
        if (beanList == null) {
            notifyDataSetChanged();
            return;
        }
        this.beanList = beanList;
    }

    @Override
    public int getCount() {
        return beanList.size() == 0 ? 0 : beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            OrderViewHolder viewHolder = new OrderViewHolder();
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.order_item, null);
                viewHolder.tv_order_code = (TextView) convertView.findViewById(R.id.tv_order_code);
                viewHolder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
                viewHolder.order_listView = (NoScrollListView) convertView.findViewById(R.id.order_listView);
                viewHolder.tv_freight = (TextView) convertView.findViewById(R.id.tv_freight);
                viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
                viewHolder.tv_goods_number = (TextView) convertView.findViewById(R.id.tv_goods_number);
                viewHolder.tv_go_pay = (TextView) convertView.findViewById(R.id.tv_go_pay);
                viewHolder.tv_cancel = (TextView) convertView.findViewById(R.id.tv_cancel);
                viewHolder.tv_delete = (TextView) convertView.findViewById(R.id.tv_delete);
                viewHolder.tv_affirm = (TextView) convertView.findViewById(R.id.tv_affirm);
                viewHolder.tv_refund = (TextView) convertView.findViewById(R.id.tv_refund);
                viewHolder.tv_refund1 = (TextView) convertView.findViewById(R.id.tv_refund1);
                viewHolder.tv_cancel1 = (TextView) convertView.findViewById(R.id.tv_cancel1);
                viewHolder.shipments = (TextView) convertView.findViewById(R.id.shipments);
                viewHolder.timeCount = (TimeViewFlicker) convertView.findViewById(R.id.timeCount);


                viewHolder.buy_two = (TextView) convertView.findViewById(R.id.buy_two);
                viewHolder.shop_progress = (TextView) convertView.findViewById(R.id.shop_progress);
                viewHolder.shop_progress2 = (TextView) convertView.findViewById(R.id.shop_progress2);
                viewHolder.tv_no_refund = (TextView) convertView.findViewById(R.id.tv_no_refund);
                viewHolder.ll_top1 = (RelativeLayout) convertView.findViewById(R.id.ll_top1);

                viewHolder.ll_time = (LinearLayout) convertView.findViewById(R.id.ll_time);

                viewHolder.order_status_1 = (RelativeLayout) convertView.findViewById(R.id.order_status_1);
                viewHolder.order_status_0 = (RelativeLayout) convertView.findViewById(R.id.order_status_0);
                viewHolder.order_status_3 = (RelativeLayout) convertView.findViewById(R.id.order_status_3);
                viewHolder.order_status_2 = (RelativeLayout) convertView.findViewById(R.id.order_status_2);
                viewHolder.order_status_6 = (RelativeLayout) convertView.findViewById(R.id.order_status_6);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (OrderViewHolder) convertView.getTag();
            }

            viewHolder.tv_order_code.setText(beanList.get(position).getOrder_no() + "");
            viewHolder.tv_type.setText(beanList.get(position).getOrder_desc());
            viewHolder.tv_goods_number.setText("共" + beanList.get(position).getCount() + "件商品");
            viewHolder.tv_price.setText(beanList.get(position).getTotal_fee());
            viewHolder.tv_freight.setText("(含运费¥ " + beanList.get(position).getExpress_fee() + ")");


            Log.e("===待付款订单时间===", "" + beanList.get(position).getLastTime());

            if (beanList.get(position).getType().equals("0")) {
                viewHolder.ll_time.setVisibility(View.GONE);
            } else {
                if (!beanList.get(position).getLastTime().equals("0")) {
                    Log.e("付款倒计时==", beanList.get(position).getLastTime());
                    Date date = new Date(Long.parseLong(beanList.get(position).getLastTime()));
                    int hh = date.getHours();
                    int mm = date.getMinutes();
                    int ss = date.getSeconds();
                    viewHolder.timeCount.startTime(hh, mm, ss);
                }
            }

            data = beanList.get(position).getData();
            mAdapter = new OrderContentAdapter(context, data);
            viewHolder.order_listView.setAdapter(mAdapter);
            index = position;
            if ("1".equals(beanList.get(position).getOrder_status())) {
                viewHolder.order_status_1.setVisibility(View.VISIBLE);
            } else if ("0".equals(beanList.get(position).getOrder_status())) {
                viewHolder.order_status_1.setVisibility(View.GONE);
                if (beanList.get(position).getType().equals("0")) {
                    viewHolder.order_status_0.setVisibility(View.VISIBLE);
                }
            } else if ("3".equals(beanList.get(position).getOrder_status())) {
                viewHolder.order_status_1.setVisibility(View.GONE);
                viewHolder.order_status_0.setVisibility(View.GONE);
                viewHolder.order_status_3.setVisibility(View.VISIBLE);
            } else if ("2".equals(beanList.get(position).getOrder_status())) {
                viewHolder.order_status_1.setVisibility(View.GONE);
                viewHolder.order_status_0.setVisibility(View.GONE);
                viewHolder.order_status_3.setVisibility(View.GONE);
                viewHolder.order_status_2.setVisibility(View.VISIBLE);
            } else if ("6".equals(beanList.get(position).getOrder_status())) {
                viewHolder.order_status_1.setVisibility(View.GONE);
                viewHolder.order_status_0.setVisibility(View.GONE);
                viewHolder.order_status_3.setVisibility(View.GONE);
                viewHolder.order_status_2.setVisibility(View.GONE);
                viewHolder.order_status_6.setVisibility(View.VISIBLE);
            } else if ("5".equals(beanList.get(position).getOrder_status())) {
                viewHolder.order_status_1.setVisibility(View.GONE);
                viewHolder.order_status_0.setVisibility(View.VISIBLE);
                viewHolder.order_status_3.setVisibility(View.GONE);
                viewHolder.order_status_2.setVisibility(View.GONE);
                viewHolder.order_status_6.setVisibility(View.GONE);
            } else if ("4".equals(beanList.get(position).getOrder_status())) {
                viewHolder.order_status_1.setVisibility(View.GONE);
                viewHolder.order_status_0.setVisibility(View.VISIBLE);
                viewHolder.order_status_3.setVisibility(View.GONE);
                viewHolder.order_status_2.setVisibility(View.GONE);
                viewHolder.order_status_6.setVisibility(View.GONE);
            }


            viewHolder.tv_go_pay.setTag(position);
            viewHolder.tv_go_pay.setOnClickListener(listener);
            viewHolder.ll_top1.setTag(position);
            viewHolder.ll_top1.setOnClickListener(listener);
            viewHolder.tv_cancel.setTag(position);
            viewHolder.tv_cancel.setOnClickListener(listener);
            viewHolder.tv_delete.setTag(position);
            viewHolder.tv_delete.setOnClickListener(listener);
            viewHolder.tv_affirm.setTag(position);
            viewHolder.tv_affirm.setOnClickListener(listener);
            viewHolder.tv_refund.setTag(position);
            viewHolder.tv_refund.setOnClickListener(listener);
            viewHolder.tv_refund1.setTag(position);
            viewHolder.tv_refund1.setOnClickListener(listener);
            viewHolder.tv_cancel1.setTag(position);
            viewHolder.tv_cancel1.setOnClickListener(listener);
            viewHolder.tv_no_refund.setTag(position);
            viewHolder.tv_no_refund.setOnClickListener(listener);
            viewHolder.buy_two.setTag(position);
            viewHolder.buy_two.setOnClickListener(listener);
            viewHolder.shop_progress.setTag(position);
            viewHolder.shop_progress.setOnClickListener(listener);
            viewHolder.shop_progress2.setTag(position);
            viewHolder.shop_progress2.setOnClickListener(listener);
            viewHolder.shipments.setTag(position);
            viewHolder.shipments.setOnClickListener(listener);

        } catch (Exception e) {

            MessageBean3 m = new MessageBean3();
            m.setMsg("0");
            EventBus.getDefault().post(m);
            mAdapter.notifyDataSetChanged();
        }

        return convertView;
    }

    class OrderViewHolder {
        TextView tv_order_code;
        TextView tv_type;
        NoScrollListView order_listView;
        TextView tv_freight;
        TextView tv_price;
        TextView tv_goods_number;
        TextView tv_go_pay;
        TextView tv_cancel;
        TextView tv_delete;
        TextView tv_affirm;
        TextView tv_refund;
        TextView tv_refund1;
        TextView tv_cancel1;
        TextView buy_two;
        TextView shop_progress;
        TextView shipments;
        TextView tv_no_refund;
        TextView shop_progress2;
        RelativeLayout ll_top1;
        TimeViewFlicker timeCount;

        RelativeLayout order_status_1;
        RelativeLayout order_status_0;
        RelativeLayout order_status_3;
        RelativeLayout order_status_2;
        RelativeLayout order_status_6;

        LinearLayout ll_time;
    }
}
